﻿@{
    Layout = null;
}
<link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
<body class="layui-padding-3">
    <input type="text" id="id" value="@ViewBag.id" hidden />
    <input type="text" id="pid" value="@ViewBag.pid" hidden />
    <div class="layui-item" style="width:500px">
        <label class="layui-label">父分类</label>
        <div class="layui-form">
            <select class="layui-select" id="parentId" name="parentId" disabled>
            </select>
        </div>
    </div>
    <div class="layui-item">
        <label class="layui-form-label">分类名称</label>
        <div class="layui-input-block">
            <input type="text"
                   id="materialTypeName"
                   name="materialTypeName"
                   lay-verify="required"
                   placeholder="请输入"
                   autocomplete="off"
                   class="layui-input" />
        </div>
    </div>
    <div class="layui-item">
        <label class="layui-form-label">显示排序</label>
        <div class="layui-input-inline" style="width: 100px">
            <input type="number"
                   lay-affix="number"
                   id="orderBy"
                   name="orderBy"
                   placeholder=""
                   autocomplete="off"
                   class="layui-input"
                   min="0"
                   step="1"
                   lay-verify="required" />
        </div>
    </div>
    <div class="layui-item">
        <label class="layui-form-label">产品/物料</label>
        <div class="layui-form">
            <input type="radio" class="layui-radio" name="products" id="productsYes" value="产品" title="产品" checked>
            <input type="radio" class="layui-radio" name="products" id="productsNo" value="物料" title="物料">
        </div>
    </div>
    <div class="layui-item">
        <label class="layui-form-label">是否启用</label>
        <div class="layui-form">
            <input type="checkbox"
                   id="state"
                   name="state"
                   lay-skin="switch"
                   lay-filter="switchTest"
                   class="layui-checkbox"
                   title="启用|禁用" checked />
        </div>
    </div>
    <div class="layui-item">
        <div class="layui-form">
            <button type="submit" id="submit" class="layui-btn" lay-submit lay-filter="demo1">
                保存
            </button>
            <button type="reset" class="layui-btn layui-btn-primary" onclick="closeWorkStation()">重置</button>
        </div>
    </div>
</body>
<script src="~/libs/jquery/jquery.js"></script>
<script src="~/libs/layui-v2.5.6/layui/layui.js"></script>
<script>

    //关闭弹框
    function closeWorkStation() {
        var index = parent.layer.getFrameIndex(window.name);//先得到当前的索引
        parent.layer.close(index);//再执行关闭
    }

    function GetAll (){
      $.ajax({
        url: '/MaterialType/GetAll',
        type: 'get',
        dataType: 'json',
        async:false,
        success: function(res) {
            $("#parentId").empty(); // 清空下拉框旧的选项
            var html = (`<option value="0">无父级</option>`);
            res.forEach(function(item) {
                // 使用 materialTypeName 添加选项
                html += (`<option value="${item.id}">${item.materialTypeName}</option>`);
            });
            console.log(html)
            $("#parentId").html(html)
            layui.form.render('select')
            GetSingType()
        },
        error: function(xhr, status, error) {
            console.error("AJAX Error: " + status + ": " + error);
        }
      });
    }

    function GetSingType (){
        var id = $("#id").val();
        var pid = $("#pid").val();
        layui.form.render('select');
        if(id != 0){
          $.ajax({
             url: '/MaterialType/GetSingMaterialType',
             data: {id:id},
             type: 'get',
             dataType: 'json',
             success: function(res) {
                 console.log(1,res);
                 $("#parentId").val(res.parentId)
                 $("#materialTypeName").val(res.materialTypeName)
                 $("#orderBy").val(res.orderBy)
                 res.products?$("#productsYes").prop('checked',true):$("#productsNo").prop('checked',true)
                 $("#state").prop('checked',res.state?"on":"off")
                 layui.form.render('select')
                 layui.form.render('radio')
                 layui.form.render('checkbox')
             }
          })
        }
        else{
            $("#parentId").val(pid);
            layui.form.render('select');
            console.log($("#parentId").val())
        }
    }

    $(()=>{
        GetAll()
    })

    layui.use(function () {
      var layer = layui.layer;
      function Insert(){
        var id = $("#id").val();
        var arr = 1;
          if($("#materialTypeName").val() == ''){
              layer.msg('请输入类型名称',{icon: 0});
              arr = 0;
              return;
          }
          if($("#orderBy").val() == ''){
              layer.msg('请输入排序',{icon: 0});
              arr = 0;
              return;
          }
        var obj = {
            id:$("#id").val(),
            parentId:$("#parentId").val(),
            materialTypeName:$("#materialTypeName").val(),
            orderBy:$("#orderBy").val(),
            products:$('[name="products"]').val(),
            state:$("#state").val() == "on",
        };
        if(arr != 0){
            if(id == 0){
                $.ajax({
                     url: '/MaterialType/MaterialTypeInsert',
                     data: obj,
                     type: 'post',
                     dataType: 'text',
                     success: function(res) {
                         if(res>0){
                             layer.msg('新增成功', { icon: 1 });
                             setTimeout(function(){closeWorkStation()},330)
                         }
                         else{
                             layer.msg('新增失败', { icon: 0 });
                         }
                     }
                })
            }
            else{
                $.ajax({
                     url: '/MaterialType/MaterialTypeUpdate',
                     data: obj,
                     type: 'post',
                     dataType: 'text',
                     success: function(res) {
                         if(res>0){
                             layer.msg('编辑成功', { icon: 1 });
                             setTimeout(function(){closeWorkStation()},330)
                         }
                         else{
                             layer.msg('编辑失败', { icon: 0 });
                         }
                     }
                })
            }
        }
      }
        $("#submit").on('click',function(){
            Insert()
        })
    })

</script>